package com.pan.behavioral.memento.implement;

import java.util.LinkedList;

public class Caretaker {
    private final static int MEMORY_LIMIT = 10;

    private class MyStack<T> {
        private LinkedList<T> li = new LinkedList<>();

        public void push(T obj) {
            limitSize();
            li.add(obj);
        }

        public T pop() {
            return li.removeLast();
        }

        private void limitSize() {
            for (; li.size() >= MEMORY_LIMIT; ) {
                li.removeFirst();
            }
        }
        private int size(){return li.size();}
    }

    private MyStack<OriginatorFrame.Memento> stack;

    {
        stack = new MyStack();
    }

    public void push(OriginatorFrame.Memento memento) {
        stack.push(memento);
    }

    public OriginatorFrame.Memento pop() {

        if (stack.size()==0)return null;
        return stack.pop();
    }
}
